(function () {


    app.section.onReady = function (params) {
        // console.log('页面传值 = ', params);
        $app.loading();
    };
    
    
    
    app.section.onLoad = function (params) {
        console.log('页面传值 = ', params);
        var id = params.id;
        $app.removeSession('personG');//本地本地存储
        var personG=[];//全局性保存再次预约的数据
    
        //取消操作
        $app.delDoGoTT=function(cancleReason){
            $ajax.accessDone(id,9,cancleReason,function(){
                $app.toast('预约已取消',true,function(){
                    ui.refresh();
                });
            });
        };
    
        //获取来拒绝用语
        var htmlTY='';
        $ajax.getCancleRreasonList(function (resData) {
            htmlTY+= '<div class="bgWhite" id="_2_cyy" >\
                <div class="paddingLi c888 f_20">请选择理由：</div>\
                    <ul class="table-view lineLeft0">';
            resData.forEach(function (val, i) {
                var text = val.content;
                htmlTY += '<li class="table-view-cell paddingL0 f_22 ">\
                        <div class="row-fluid">\
                            <div class="span2 text-center">\
                                <i class="iconfont iconquan cCCC f_30 relative" data-mark="icon"></i>\
                            </div>\
                            <div class="span10 ">'+ text + '</div>\
                        </div>\
                    </li>';
            });
            htmlTY += '<li class="table-view-cell paddingL0 f_22">\
                        <div class="row-fluid">\
                            <div class="span2 text-center">\
                                <i class="iconfont iconquan cCCC f_30 relative" data-mark="icon" style="top:10px"></i>\
                            </div>\
                            <div class="span10 ">\
                                <input type="text"  placeholder="请输拒绝或取消来访订单的理由" class=" input-large c888 borderNone f_22"/>\
                            </div>\
                        </div>\
                    </li>\
                </ul>\
                <div class="row-fluid">\
                    <div class="span12 paddingLi">\
                        <button type="button" data-role="BTButton" data-inline="true" data-theme="a" class="width100 paddingLi lineBlue borderNone" onclick="$app._2_cyy()">确定</button>\
                    </div>\
                </div>\
            </div>';
        });
    
        
    
        //常用语确定函数
        $app._2_cyy=function() {
            var li = $('#_2_cyy').find('li');
            var jud = false;//判断是否选中
            var who = '';
            var zhi = ''
            li.each(function (i, val) {
                var icon = $(val).find('[data-mark="icon"]');
    
                //蓝色高亮，是我选中那个
                if (icon.hasClass('cBlue')) {
                    jud = true;
                    who = icon.parent().siblings();
    
                }
            });
    
            if (!jud) {
                $app.alert('请选择取消订单理由');
            } else {
                if ($(who).find('input').length > 0) {
                    zhi = $app.inputVal($(who).find('input'));
                } else {
                    zhi = $(who).text();
                }
                if ($app.judgeData(zhi)) {
                    ui.closePopup();
                    $app.delDoGoTT(zhi);
                } else {
                    $app.alert('请选择取消订单理由');
                }
            }
        };
        //来访事由单选判断
        $(document).on('tap', '#_2_cyy li', function () {
            var icon = $(this).find('[data-mark="icon"]');
            if (icon.hasClass('cBlue')) {
                console.log('必须选择1个，不给你取消操作')
            } else {
                icon.addClass('cBlue icon-checkmark').removeClass('cCCC iconfont iconquan');
                //单选操作
                icon.parents('li').siblings().find('[data-mark="icon"]').addClass('cCCC iconfont iconquan').removeClass('cBlue icon-checkmark');
            }
        });
    
    
        //取消当前订单
        $('#_2_detail_x').on('tap',function(){
            var button=$(this);
            var status=button.attr('data-status');
            var text=button.text();
    
            if(status==0){
                if(text!='已同意申请'){
                    $app.confirm('取消预约来访？', '温馨提示', {
                        yesText: '确定',
                        yesFun: function () {
                            ui.popup({
                                html: htmlTY,
                                pos: 'bottom',
                                showCloseBtn: false
                            });
                        }
                    });
                }
                
            }else if(status==1){
                personGG();
            }else if(status==4){
                $app.confirm('审批通过，同意来访？', '温馨提示', {
                    yesFun: function () {
                        $ajax.accessDone(id,0,'',function(){
                            $app.toast('审核通过',true,function(){
                                ui.refresh();
                            });
                        });
                    }
                });
            }else if(status==9){
                personGG();
            }
            
        });
    

        function personGG(){
            //本地存储，然后倒回去首页
            $app.setSession(personG, 'personG');
            $app.loading();
            app.refresh();
        }
    
        
        /**
         * @template 人员信息
         * @param { string } name 
         * @param { string } id 
         * @param { string } phone 
         * @param { 1 | 2 | 3 } type 人员类型 1. 重点人员 2.临控人员 3.一般人员 
         */
         function VisitorList(name, id, phone, type){

                const conf = {
                    1: '重点人员',
                    2: '临控人员'
                }
                
                const warningLabel = conf[type] 
                const classType = warningLabel ? 'huaiRen' :'';
                
                /**
                 * @template 特殊人员信息头
                 * @param { string } label 特殊类型名
                 */
                function Warning(label){
                    return label ? `<span class='c888' style='color: #333 !important'>【${label}】</span> ` : ''
                }


                return `
                
                    <li class="table-view-cell f_24 ${classType}">
                        <div class="row-fluid marginB10">
                            <div class="span3 text-left paddingR10 ">姓 名:</div>
                            <div class="span9 c888" style='margin-left: -20px'>
                                ${name}
                                ${Warning(warningLabel)}
                            </div>
                        </div>
                        <div class="row-fluid marginB10">
                            <div class="span3 text-left paddingR10">身份证:</div>
                            <div class="span9 c888 stringBug3" style='margin-left: -20px'>${id}</div>
                        </div>
                        <div class="row-fluid margin0">
                            <div class="span3 text-left paddingR10">手机号:</div>
                            <div class="span9 c888 stringBug3" style='margin-left: -20px'>${phone}</div>
                        </div>
                    </li>
                
                `
                
                
            }

            /**
             * @template 头部信息
             * @param { string } status 来访状态
             * @param { string } time 来访时间
             * @param { string } cause 事由
             * @param { string } source 来源
             * @param { string } unit 部门单位
             * @param { string } typeColor 类型颜色
             * @param { string } slot 模板插槽
             */
            function Head({ status, time, cause, source, unit, typeColor, slot }){

                return `

                    <li class="table-view-cell f_24 ">
                        <div class="row-fluid marginB10">
                            <div class="span3 text-left paddingR10">状 态:</div>
                            <div class="span9 text-left"  style='margin-left: -30px; color: ${typeColor}' >${ status }</div>
                        </div>
                        <div class="row-fluid marginB10">
                            <div class="span3 text-left paddingR10">时 间:</div>
                            <div class="span9 c888 stringBug3" style='margin-left: -30px'>${ time }</div>
                        </div>
                        <div class="row-fluid marginB10">
                            <div class="span3 text-left paddingR10">事 由:</div>
                            <div class="span9 c888 " style='margin-left: -30px'>${ cause }</div>
                        </div>
                        <div class="row-fluid marginB10">
                            <div class="span3 text-left paddingR10">来 源:</div>
                            <div class="span9 c888 " style='margin-left: -30px'>${ source }</div>
                        </div>
                        <div class="row-fluid margin0">
                            <div class="span3 text-left paddingR10">单 位:</div>
                            <div class="span9 c888 " style='margin-left: -30px'>${ unit }</div>
                        </div>
                        ${slot}
                    </li>
                    <li class="nothingDo"></li>
                    <li class="table-view-cell f_20 c999 paddingLi"><span class="paddingL10">来访人员信息：</span></li>

                
                `

            }

            /**
             * @function 门卫确认时间
             * @param { string } time 确认时间
             */

             function guard(time){

                return `
                
                    <div class="row-fluid marginB10" style='margin-top: 10px'>
                        <div class="span5 text-left paddingR10">门卫确认时间:</div>
                        <div class="span7 c888 " style='margin-left: -30px'>${ time }</div>
                    </div>
                
                `

             }
        
    
        
        
        //获取详情
        $ajax.accessDetail(id, function (res) {
            var htmlK='';
            var status=res.data.status;
            var cancle_reason=res.data.cancle_reason;
            var isWeChat=res.data.isWeChat;
            const currentStatus = $app.VISIT_STATUS(status)
            
            
            var button=$('#_2_detail_x');
            button.attr({
                'data-status':status,
                'data-wx':isWeChat
            });
    
            //返回+拒绝按钮
            $('#_2_detail_Y').on('tap',function(){
                if(status==4){
                    $app.confirm('审批不通过，拒绝来访申请？', '温馨提示', {
                        yesFun: function () {
                            ui.popup({
                                html: htmlTY,
                                pos: 'bottom',
                                showCloseBtn: false
                            });
                        }
                    });
                }else{
                    ui.Page.back();
                }
            });
    
            //控制取消按钮  变成再次预约
            switch(parseInt(status)){
                case 0:
                    if(!!isWeChat){
                        button.prop('disabled',true).text('已同意申请');
                    }else{
                        button.addClass('lineBlack').text('取消来访');
                    }
                    
                break;
                case 1:
                    button.addClass('lineBlue').text('再次预约');
                break;
                case 4:
                    button.addClass('lineRed').text('同 意');
                    $('#_2_detail_Y').text('拒 绝');
                break;
                case 9:
                    button.addClass('lineBlue').text('再次预约');
                break;
            }
            
    
    
            var pOne=res.persons[0];
            //事由
            personG.push({
                reason:res.data.reason
            });
            //来访人信息赋值给全局
            (res.persons).forEach(function(val,i){
                personG.push({
                    name: val.name,
                    idCard: val.idCard,
                    phone: val.phone,
                    unit:val.unit
                });
            });



            rkT(status == 4 || !!isWeChat);
            
            
            /**
             * @template 构建来访人列表
             * @param { boolean } check 是否做人员校验 
             */
            function rkT(check){

                var cancleT=(status==9?'<span class="c666 marginL10">( '+cancle_reason+' )</span>':'');
                
                htmlK = Head({

                    status: currentStatus.label + cancleT,
                    time: $app.delSecond(res.data.accessTime),
                    cause: res.data.reason,
                    source: !!isWeChat?'微信申请':'警员申请',
                    unit: res.persons[0].unit || ' - ',
                    typeColor: currentStatus.color,
                    slot: !!res.data.sureTime ? guard( $app.delSecond(res.data.sureTime) ) : ''
                    // slot: guard( $app.delSecond( $app.delSecond(res.data.accessTime) ))
                    
                })

                $('#_2_detailMain').html(htmlK);

                res.persons.forEach((val, i) =>{

                    const { name, idCard, phone } = val
                    const getType = check ?  (fn) =>(fn("3")) : (fn) => $app.juPersonS(idCard, e => fn(parseInt(e.who)));
                    const joinTmp = (type) => {
                        const htmlTmp =  VisitorList(name, idCard, phone, type)
                        $('#_2_detailMain').append(htmlTmp);
                     };
                    getType(joinTmp)

                    
                })

                
                
            }
    
            
        });
    
        
    };
    
    app.section.onBack = function () {
        console.log('我回去了!');
        //刷新上一级
        $app.exReload();
    };
    
    
})()